import Vue from 'vue';
import Vuex from 'vuex';

Vue.use(Vuex)

export default new Vuex.Store({
    state: {
        // menuList:[],
        menuList: [
            {
                //一级菜单
                title: '应用',
                index: '/app',
                icon: 'el-icon-menu',
                //二级菜单
                children: [
                    {
                        title: '会员中心',
                        index: '/mem',
                        icon: 'el-icon-menu',
                        children: [
                            {
                                title: '会员中心',
                                index: '/MembersView',
                            },
                            {
                                title: '会员积分',
                                index: '/MemberPointHistoryView',
                            },
                            {
                                title: '会员积分等级',
                                index: '/MemberLevelView',
                            },
                            {
                                title: '会员积分策略',
                                index: '/MemberRewardStrategyView',
                            }
                        ],
                    },
                    {
                        title: 'Iot物联网平台',
                        index: '/IotSchedu',
                        icon: 'el-icon-menu',
                        children: [
                            {
                                title: 'Iot设备管理',
                                index: '/IotAdminView',
                            },
                        ],
                    },
                    {
                        title: 'HCM网站',
                        index: '/hvmfood',
                        icon: 'el-icon-menu',
                        children: [
                            {
                                title: '网站留言',
                                index: '/WebSubjectView',
                            },
                            {
                                title: '活动项目',
                                index: '/WebProjectCampView',
                            },
                            {
                                title: '注册会员',
                                index: '/MemberWeightView'
                            },
                            {
                                title: '餐食计算',
                                index: '/FoodCalDinnerMapView',
                            },
                            {
                                title: '固定食谱',
                                index: '/CustDinnerMapView',
                            },
                            {
                                title: '食材配置',
                                index: '/HealthyFoodCatalogsView',
                            },
                            {
                                title: '补充打印',
                                index: '/supplyPrintingView',
                            },
                            // {
                            //     title: '减肥计划',
                            //     index: '/HealthyCateringMatchingView',
                            // },
                        ],
                    },
                    {
                        title: '数维计算',
                        index: '/purm',
                        icon: 'el-icon-menu',
                        children: [
                            {
                                title: '文字配色',
                                index: '/ArticalDesFormView',
                            },
                            {
                                title: '表单设计器',
                                index: '/VdesFormView',
                            },
                            {
                                title: '生命耀州 Free Life',
                                index: '/PurmFreelifeView',
                            },
                            {
                                title: '生命耀州基础标准表',
                                index: '/PurmFreelifeVDBView',
                            },
                            {
                                title: '常青财富收益',
                                index: '/PurmEvergreenWealthIncomeView',

                            },
                            {
                                title: '多元货币计划',
                                index: '/PurmMultiCurrencyPlanView'
                            },
                        ],
                    },
                    {
                        title: '邮件&任务',
                        index: '/MailSedu',
                        icon: 'el-icon-menu',
                        children: [
                            {
                                title: '邮件发送记录',
                                index: '/mails',
                            },
                            {
                                title: '定时任务',
                                index: '/QuartzCal',
                            },
                        ],
                    },
                ],
            },
            {
                //一级菜单
                title: '用户配置',
                index: '/user',
                icon: 'el-icon-user-solid',
                //二级菜单
                children: [
                    {
                        title: '我的设置',
                        index: '/mine',
                        children: [
                            {
                                title: '基本资料',
                                index: '/basic',
                            },
                            {
                                title: '修改密码',
                                index: '/changePwd',
                            },

                        ],
                    },
                ],
            },
            {
                //一级菜单
                title: '设置',
                index: '/set',
                icon: 'el-icon-setting',
                //二级菜单
                children: [
                    {
                        title: '系统设置',
                        index: '/system',
                        //三级菜单
                        children: [
                            {
                                title: '网站设置',
                                index: '/web',
                            },
                            {
                                title: '系统字典',
                                index: '/SysDictsView',
                            },
                            {
                                title: '邮件服务参数',
                                index: '/email',
                            },
                            {
                                title: '打印机参数',
                                index: '/PrintSettingView',
                            },
                        ],
                    },
                    {
                        title: '安全管理',
                        index: '/secure',
                        children: [
                            {
                                title: '角色管理',
                                index: '/roles',
                            },
                            {
                                title: '权限管理',
                                index: '/permissions',
                            },
                            {
                                title: '用户管理',
                                index: '/users',
                            },
                        ],
                    },
                ],
            },
        ],
        activeName: 'Home',
        tabList: [
            /* {
                title: '编辑文章',
                component: 'Edit',
                name: '/edit',
            }, */
        ],
        activeNav: '/home'
    },
    // getters
    getters: {
        getMenuList(state) {
            return state.menuList
        }
    },
    mutations: {
        add_tab(state, val) {      //val：导航栏点击获得的路径，即要在标签页添加的
            state.activeNav = val
            //判断已经打开的tabList里是否已存在要添加的页面，true表示不存在，false表示存在
            let flag = true
            let titname = ''
            // name = name.replace('/', "")
            if ('/home' === val) {
                state.activeName = 'Home'
                flag = false
                return
            }
            state.tabList.forEach((element) => {
                if (element.name === val) {
                    state.activeName = element.component
                    flag = false
                    return
                }
            })
            if (flag) {
                state.menuList.forEach((element) => {
                    if (element.children.length > 0) {
                        element.children.forEach((e) => {
                            if (e.children.length > 0) {
                                e.children.forEach((echidren) => {
                                    if (echidren.index === val) {
                                        titname = echidren.title
                                    }
                                })
                            } else {
                                if (e.index === val) {
                                    titname = e.title
                                }
                            }
                        })
                    } else {
                        if (element.index === val) {
                            titname = element.title
                        }
                    }
                })
                let component = (val.replace('/', '')).substring(0, 1).toUpperCase() + (val.replace('/', '')).substring(1)
                // console.log(component);
                state.tabList.push({
                    component: component,
                    title: titname,
                    name: val,
                })
                state.activeName = component
            }
        },
        remove_tab(state, val) {   //val：要删除的tab的属性中的name，即component
            let tabList = state.tabList
            tabList.forEach((tab, index) => {
                if (tab.component === val) {
                    let nextTab = tabList[index + 1] || tabList[index - 1]
                    state.tabList.splice(index, 1)
                    if (nextTab) {
                        state.activeName = nextTab.component
                        state.activeNav = nextTab.name
                    } else {
                        state.activeName = 'Home'
                        state.activeNav = '/home'
                    }
                }
            })
        },
        set_active_name(state, val) { //val：当前tab的name
            state.activeName = val
        },
    },
    actions: {}
})